/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/10/22
 */
layui.define(['form', 'layedit'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;

    form.render(null, "zmn-form");

    // 自定义验证规则
    form.verify({
       isPositiveNumber: function(value,item){
           if (!/^[1-9]\d*$/.test(value)) {
                return "排序值只能是正整数";
            }
           if(Number(value)>100000){
               return "排序最大100000";
           }
        }
    });

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        let nodesArr = new Array();
        let typeArr = new Array();
        let bizTypeArray = new Array();

        let node = $('input[name="node"]');
        for(let k in node){
            if(node[k].checked)
                nodesArr.push(node[k].value);
        }
        if(nodesArr.length == 0){
            layer.msg("取消节点不能为空");
            return false;
        }
        let type = $('input[name="type"]');
        for(let k in type){
            if(type[k].checked)
                typeArr.push(type[k].value);
        }
        if(typeArr.length == 0){
            layer.msg("取消类型不能为空");
            return false;
        }
        let bizType = $('input[name="bizType"]');
        for(let k in bizType){
            if(bizType[k].checked)
                bizTypeArray.push(bizType[k].value);
        }
        if(bizTypeArray.length == 0){
            layer.msg("业务类型不能为空");
            return false;
        }

        var openId = layer.open({type: 3});
        var data = obj.field;
        var url = "/orderconfig/cancelReason/addEdit.action";
        data.bizType = bizTypeArray.join(",");
        data.node = nodesArr.join(",");
        data.type = typeArr.join(",");

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(result) {
                layer.close(openId);
                if(result.status == 200) {
                    layer.msg(result.message,{time: 1000},()=>{
                        location.reload();
                    });
                } else {
                    layer.msg(result.message, {"icon": 2});
                }


            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                layer.close(openId);
            }
        })
        return false;
    });
    //对外暴露的接口
    exports('cancel_reason_detail_js', {});
});
